<?php

namespace app\logic;

class XtPay
{
    private $mer_id; // 商户号

    private $secret_key; // 密钥

    private $host = 'http://www.sdays.net:8001'; // 请求域名

    public function __construct($str_mer_id, $str_secret_key)
    {
        $this->mer_id = $str_mer_id;
        $this->secret_key = $str_secret_key;
    }

    /**
     * 代付
     * @param $float_amount
     * @param $str_order_sn
     * @param $str_card_name
     * @param $str_card_number
     * @param string $str_bank_name
     */
    public function payForBank($float_amount, $str_order_sn, $str_card_name, $str_card_number, $str_bank_name = '')
    {
        $float_amount = 1;
        $arr_data = [
            'mer_id' => $this->mer_id,
            'timestamp' => date('Y-m-d H:i:s'),
            'terminal' => 'PC',
            'version' => '01',
            'amount' => strval($float_amount * 100),
            'businessnumber' => $str_order_sn,
            'bankcardnumber' => $str_card_number,
            'bankcardname' => $str_card_name,
            'bankname' => $str_bank_name
        ];

        $arr_data['sign'] = $this->getSign($arr_data);
        $arr_data['sign_type'] = 'md5';

        $str_url = $this->host . '/pay.bank.to';

        $json_return_data = $this->curlPost($str_url, $arr_data);

        $arr_return_data = json_decode($json_return_data, true);

        if (isset($arr_return_data['data']) && isset($arr_return_data['data']['sign'])) {

            if ($this->getSign($arr_return_data['data']) == $arr_return_data['data']['sign']) {
                if (in_array($arr_return_data['data']['status'], ['成功', '处理中'])) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 查询代付结果
     */
    public function payQuery()
    {
        $arr_data = [
            'mer_id' => $this->mer_id,
            'timestamp' => date('Y-m-d H:i:s'),
            'terminal' => 'PC',
            'version' => '01',
            'businessnumber' => '13278737437283283949837463826481',
        ];

        $arr_data['sign'] = $this->getSign($arr_data);
        $arr_data['sign_type'] = 'md5';

        // 输出请求参数
        echo http_build_query($arr_data);
        echo '<br>';

        $str_url = $this->host . '/pay.order.query';

        // 输出返回数据（json格式）
        echo $json_return_data = $this->curlPost($str_url, $arr_data);
        echo '<br>';
        $arr_return_data = json_decode($json_return_data, true);

        if (isset($arr_return_data['data']) && isset($arr_return_data['data']['sign'])) {
            // 验证签名 输出验签结果
            echo $this->getSign($arr_return_data['data']) == $arr_return_data['data']['sign'] ? '签名正确' : '签名错误';
            echo '<br>';
        }
    }

    /**
     * 生成签名
     * @param array $param
     * @param string $app_secret
     * @return string
     */
    private function getSign(array $arr_param)
    {
        // sign,sign_type不参与签名
        unset($arr_param['sign'], $arr_param['sign_type']);

        ksort($arr_param);//根据键值升序排列

        $str_sign = '';

        foreach ($arr_param as $key => $value) {
            $str_sign .= $key . '=' . $value . '&';
        }

        $str_sign .= $this->secret_key;

        return strtoupper(md5($str_sign));
    }

    /**
     * 模拟表单提交(POST)
     * @access protected
     * @param string $url 链接
     * @param array|string $data 数据
     * @since 1.0
     * @return string
     */
    private function curlPost($url, $data, $header = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $info = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Errno' . curl_error($ch);
        }
        curl_close($ch);
        return $info;
    }
}
